package com.finalproject.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JOptionPane;

import com.finalproject.ui.UIHelper;

/**
 * The purpose of this class represent VendingMachine buy snackItem add
 * snackItem remove snackItem retreive snackItem
 * 
 * @author dave
 * @version 02/25/2012 (cagri)
 * 
 */
public class VendingMachine {

	private int id;
	private int[] coordinates = new int[2];

	private Map<String, List<SnackItem>> snackItemMap;

	public interface VendingMachineObserver {
		public void onUpdate(VendingMachine vendingMachine);
	}

	public List<VendingMachineObserver> vendingMachineObservers;

	public VendingMachine(int id, int[] coordinates) {
		this.id = id;
		snackItemMap = new HashMap<String, List<SnackItem>>();
		this.coordinates = coordinates;
		this.vendingMachineObservers = new ArrayList<VendingMachineObserver>();

	}

	public void addVendingmachineObserver(
			VendingMachineObserver vendingMachineObserver) {
		if(vendingMachineObservers == null) {
			vendingMachineObservers = new  ArrayList<VendingMachine.VendingMachineObserver>();
		}
		if (!vendingMachineObservers.contains(vendingMachineObserver)) {
			vendingMachineObservers.add(vendingMachineObserver);
		}

	}

	public void removeVendingMachineObserver(
			VendingMachineObserver vendingMachineObserver) {
		vendingMachineObservers.remove(vendingMachineObserver);
	}

	private void fireVendingMachineObserverUpdate(VendingMachine vendingMachine) {

		for (VendingMachineObserver vendingMachineObserver : vendingMachineObservers) {
			vendingMachineObserver.onUpdate(vendingMachine);
		}
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int[] getCoordinates() {
		return coordinates;
	}

	public void setCoordinates(int[] coordinates) {
		this.coordinates = coordinates;
	}

	/**
	 * add snackItem
	 * 
	 * @param snackItem
	 */

	public void add(SnackItem snackItem, int howMany) {
		for (int i = 0; i < howMany; i++) {
			SnackItem clone = snackItem.clone();
			add(clone);
		}
	}

	// ///should we change it to add(SnackItem snackItem, int howmany)??
	public void add(SnackItem snackItem) {
		String name = snackItem.getName();
		List<SnackItem> snackItems = snackItemMap.get(name);
		if (snackItems == null) {
			snackItems = new ArrayList<SnackItem>();
		}
		snackItems.add(snackItem);
		snackItemMap.put(name, snackItems);
	}

	/**
	 * get all snack names
	 * 
	 * @return
	 */
	public Set<String> getSnackItemNames() {
		return snackItemMap.keySet();
	}

	/**
	 * get snack item count by name
	 * 
	 * @param name
	 * @return
	 */
	public int getSnackItemsCountByName(String name) {
		List<SnackItem> snackItems = snackItemMap.get(name);
		if (snackItems == null) {
			return 0;
		}
		return snackItems.size();
	}

	public List<SnackItem> getSnackItemsByName(String name) {
		List<SnackItem> snackItems = snackItemMap.get(name);
		return snackItems;
	}

	/**
	 * Buy snackItem.
	 * 
	 * @param name
	 * @param howMany
	 */
	public SnackItem buySnackItem(String name) throws VendingMachineException {

		List<SnackItem> snackItems = snackItemMap.get(name);
		List<SnackItem> buyItems = new ArrayList<SnackItem>();
		if (snackItems.size() == 0) {
			throw new VendingMachineException("Insufficent SnackItems");
		}

		SnackItem snackItemReturn = null;

		for (SnackItem snackItem : snackItems) {
			buyItems.add(snackItem);
			snackItemReturn = snackItem;
			snackItems.remove(snackItem);

			break;

		}

		fireVendingMachineObserverUpdate(this);
		return snackItemReturn;

	}

	public class VendingMachineException extends Exception {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public VendingMachineException(String name) {
			super(name);
		}
	}

	

}
